﻿package com.shuqi.third;

import java.io.IOException;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

import com.weibo.sdk.android.Oauth2AccessToken;
import com.weibo.sdk.android.Weibo;
import com.weibo.sdk.android.WeiboAuthListener;
import com.weibo.sdk.android.WeiboDialogError;
import com.weibo.sdk.android.WeiboException;
import com.weibo.sdk.android.WeiboParameters;
import com.weibo.sdk.android.api.UsersAPI;
import com.weibo.sdk.android.net.RequestListener;

public class SinaFunction {

    // 新浪
    private static final String CONSUMER_KEY = "3453328089"; // 替换为开发者的appkey

    private static final String REDIRECT_URL = "http://shuqi.com"; // 回调地址

    public static Oauth2AccessToken accessToken;

    /**
     * 新浪登录验证
     * 
     * @param context 上下文 isShow 是否获取个人信息 true获取 listener
     * */
    public static void login(final Activity activity, final boolean isShow,
            final FunctionListener listener) {
        Log.e("sina", "login() isShow= " + isShow);
        if (listener == null || activity == null) {
            return;
        }
        Weibo mWeibo = Weibo.getInstance(CONSUMER_KEY, REDIRECT_URL);
        WeiboParameters parameters = new WeiboParameters();
        parameters.add("forcelogin", "true");
        mWeibo.startDialog(activity, parameters, new WeiboAuthListener() {

            @Override
            public void onComplete(Bundle values) {
                String access_token = values.getString("access_token");
                String expires_in = values.getString("expires_in");
                accessToken = new Oauth2AccessToken(access_token, expires_in);

                Log.e("sina", "onComplete() access_token= " + access_token + "_expires_in= "
                        + expires_in);

                if (accessToken.isSessionValid()) {
                    String uid = values.getString("uid");
                    listener.onComplete(uid);
                    Log.e("sina", "onComplete() accessToken 可用。uid=" + uid + "_isShow= " + isShow);
                    // 获取用户信息部分
                    if (isShow) {
                        UsersAPI user = new UsersAPI(accessToken);

                        user.show(Long.parseLong(uid), new RequestListener() {

                            @Override
                            public void onComplete(final String arg0) {
                                Log.i("sina", "user.show.onComplete arg0=" + arg0);
                                try {
                                    JSONObject jsonRoot = new JSONObject(arg0);
                                    listener.onShow(jsonRoot.optString("screen_name"),
                                            jsonRoot.optString("idstr"));
                                } catch (JSONException e1) {
                                    listener.onError();
                                }
                            }

                            @Override
                            public void onError(WeiboException e) {
                                listener.onError();
                            }

                            @Override
                            public void onIOException(IOException e) {
                                listener.onError();
                            }

                        });
                    }// end isShow
                } else {
                    listener.onError();
                }
            }

            @Override
            public void onWeiboException(WeiboException error) {
                Log.e("sina", "onError-----1111");
                listener.onError();
            }

            @Override
            public void onError(WeiboDialogError error) {
                Log.e("sina", "onError-----2222" + error.getMessage());
                listener.onError();
            }

            @Override
            public void onCancel() {
                Log.e("sina", "onError-----33333");
                listener.onError();
            }

        });

    }
}
